<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Link dialog window.
-->
<html>
<head>
	<title>Link Properties</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="robots" content="noindex, nofollow" />
	<script type="text/javascript">

var oEditor		= window.parent.InnerDialogLoaded() ;
var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;
var FCKRegexLib	= oEditor.FCKRegexLib ;
var FCKTools	= oEditor.FCKTools ;

document.write( '<script src="' + FCKConfig.BasePath + 'dialog/common/fck_dialog_common.js" type="text/javascript"><\/script>' ) ;

	</script>
	<script type="text/javascript">

// oLink: The actual selected link in the editor.
var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
if ( oLink )
	FCK.Selection.SelectNode( oLink ) ;

var bLinkEqualsName	= false ;
	
window.onload = function()
{
	// Translate the dialog box texts.
	oEditor.FCKLanguageManager.TranslatePage(document) ;

	// Load the selected link information (if any).
	LoadSelection() ;

	// Activate the "OK" button.
	window.parent.SetOkButton( true ) ;
	window.parent.SetAutoSize( true ) ;
}

function LoadSelection()
{
	if ( !oLink ) return ;

	// Get the actual Link href.
	var sHRef = oLink.getAttribute( '_fcksavedurl' ) ;
	if ( sHRef == null )
		sHRef = oLink.getAttribute( 'href' , 2 ) || '' ;
		
	if ( sHRef == oLink.innerHTML )
		bLinkEqualsName = true ;

	GetE('txtUrl').value = sHRef ;
}

var searchTimer ;

//#### Called while the user types the URL.
function OnUrlChange()
{
	var link = GetE('txtUrl').value.Trim() ;

	if ( searchTimer )
		window.clearTimeout( searchTimer ) ;
		
	if ( link.StartsWith( '#' ) )
	{
		SetSearchMessage( FCKLang.wikiDlgLinkNosearchAn || 'anchor link... no search for it' ) ;
		return ;
	} 
	
	if ( link.StartsWith( 'mailto:' ) )
	{
		SetSearchMessage( FCKLang.wikiDlgLinkNosearchMail || 'e-mail link... no search for it' ) ;
		return ;
	} 
	
	if( /^\w+:\/\//.test( link ) )
	{
		SetSearchMessage( FCKLang.wikiDlgLinkNosearchExt || 'external link... no search for it' ) ;
		return ;
	}

	if ( link.length < 3  )
	{
		ClearSearch() ;

		if ( link.length == 0 )
			SetSearchMessage( FCKLang.wikiDlgLinkStartTyping || 'start typing in the above field' ) ;
		else
			SetSearchMessage( FCKLang.wikiDlgLinkTooShort || 'too short... type more' ) ;
		return ;
	}
	
	SetSearchMessage( FCKLang.wikiDlgLinkStopTyping || 'stop typing to search' ) ;
	searchTimer = window.setTimeout( StartSearch, 500 ) ;
}

function StartSearch()
{
	var link = GetE('txtUrl').value.Trim() ;

	if ( link.length < 3  )
		return ;

	SetSearchMessage( FCKLang.wikiDlgLinkSearching ||  'searching...' ) ;

	// Make an Ajax search for the pages.
	oEditor.window.parent.sajax_request_type = 'GET' ;
	oEditor.window.parent.sajax_do_call( 'wfSajaxSearchArticleFCKeditor', [link], LoadSearchResults ) ;
}

function LoadSearchResults( result )
{
	var results = result.responseText.Trim().split( '\n' ) ;
	var select = GetE( 'xWikiResults' ) ;
	
	ClearSearch() ;

	if ( results.length == 0 || ( results.length == 1 && results[0].length == 0 ) )
	{
		SetSearchMessage( FCKLang.wikiDlgLinkNoArticle || 'no articles found' ) ;
	}
	else
	{
		if ( results.length == 1 )
			SetSearchMessage( FCKLang.wikiDlgLinkOneArticle || 'one article found' ) ;
		else
			SetSearchMessage( results.length + FCKLang.wikiDlgLinkManyArticle || ' articles found' ) ;

		for ( var i = 0 ; i < results.length ; i++ )
			FCKTools.AddSelectOption( select, results[i], results[i] ) ;
	}
}

function ClearSearch()
{
	var select = GetE( 'xWikiResults' ) ;

	while ( select.options.length > 0 )
		select.remove( 0 ) 
}

function SetSearchMessage( message )
{
	GetE('xWikiSearchStatus').innerHTML = message ;
}

function SetUrl( url )
{
	GetE('txtUrl').value = url ;
}

//#### The OK button was hit.
function Ok()
{
	var sUri = GetE('txtUrl').value ;
	var sInnerHtml ;

	// If no link is selected, create a new one (it may result in more than one link creation - #220).
	var aLinks = oLink ? [ oLink ] : oEditor.FCK.CreateLink( sUri ) ;

	// If no selection, no links are created, so use the uri as the link text (by dom, 2006-05-26)
	var aHasSelection = ( aLinks.length > 0 ) ;
	if ( !aHasSelection )
	{
		sInnerHtml = sUri;

		var oLinkPathRegEx = new RegExp("//?([^?\"']+)([?].*)?$") ;
		var asLinkPath = oLinkPathRegEx.exec( sUri ) ;
		if (asLinkPath != null)
			sInnerHtml = asLinkPath[1];  // use matched path
		
		// Create a new (empty) anchor.
		aLinks = [ oEditor.FCK.InsertElement( 'a' ) ] ;
	}

	oEditor.FCKUndo.SaveUndoStep() ;

	for ( var i = 0 ; i < aLinks.length ; i++ )
	{
		oLink = aLinks[i] ;

		if ( aHasSelection )
			sInnerHtml = oLink.innerHTML ;		// Save the innerHTML (IE changes it if it is like an URL).

		oLink.href = sUri ;
		SetAttribute( oLink, '_fcksavedurl', sUri ) ;

		if ( bLinkEqualsName )
			oLink.innerHTML = sUri ;
		else
			oLink.innerHTML = sInnerHtml ;		// Set (or restore) the innerHTML

	}

	// Select the (first) link.
	oEditor.FCKSelection.SelectNode( aLinks[0] );

	return true ;
}

	</script>
</head>
<body scroll="no" style="overflow: hidden">
	<div id="divInfo">
		<div id="divLinkTypeUrl">
			<span>Link</span><br />
			<input id="txtUrl" style="width: 100%" type="text" onkeyup="OnUrlChange();" />
			<br />
                        <span fcklang="wikiDlgLinkAutoRes">Automatic search results</span> (<span fcklang="wikiDlgLinkStartTyping" id="xWikiSearchStatus">start typing in the above field</span>)<br />
			<select id="xWikiResults" size="10" style="width: 100%; height:150px" onclick="SetUrl( this.value );">
			</select>
		</div>
	</div>
</body>
</html>
